﻿
namespace ATA.Windows.Forms
{
	/// <summary>
	/// abstract ChuongTrinh typed datagridview
	/// </summary>
	[System.ComponentModel.DesignerCategoryAttribute("code")]
	public abstract class ChuongTrinhDataGridViewBase : System.Windows.Forms.UserControl
	{	
		/// <summary> 
		/// Thrown when the current Entity changed.
		/// </summary>
		public event System.EventHandler<ChuongTrinhDataGridViewEventArgs> CurrentEntityChanged;
		
		#region Fields
				
		/// <summary> 
		/// the current entity.
		/// </summary>
		private Entities.ChuongTrinh _currentChuongTrinh = null;
		//private Dictionary<string, string> _columnsHeaders = new Dictionary<string, string>();
		
		/// <summary> 
		/// the datagridview instance
		/// </summary>
		protected System.Windows.Forms.DataGridView uxChuongTrinhDataGridView;
		
		/// <summary> 
		/// the errorprovider
		/// </summary>
		protected System.Windows.Forms.ErrorProvider uxChuongTrinhErrorProvider;
		
		/// <summary> 
		/// the bindingsource
		/// </summary>
		protected System.Windows.Forms.BindingSource uxChuongTrinhBindingSource;
		
		
		/// <summary> 
		/// the DGV column associated with the ChuongTrinhId property
		/// </summary>
		protected System.Windows.Forms.DataGridViewTextBoxColumn uxChuongTrinhIdDataGridViewColumn;
		
		/// <summary> 
		/// the DGV column associated with the TenChuongTrinh property
		/// </summary>
		protected System.Windows.Forms.DataGridViewTextBoxColumn uxTenChuongTrinhDataGridViewColumn;
		
		/// <summary> 
		/// the DGV column associated with the IsActive property
		/// </summary>
		protected System.Windows.Forms.DataGridViewCheckBoxColumn uxIsActiveDataGridViewColumn;
		
		/// <summary> 
		/// the DGV column associated with the LastUpdate property
		/// </summary>
		protected System.Windows.Forms.DataGridViewTextBoxColumn uxLastUpdateDataGridViewColumn;
		
		/// <summary> 
		/// the DGV column associated with the UpdateBy property
		/// </summary>
		protected System.Windows.Forms.DataGridViewTextBoxColumn uxUpdateByDataGridViewColumn;
		
		#endregion
					
		#region Combo list
		
		#endregion
		
		#region Main Datasource
		
		private Entities.TList<Entities.ChuongTrinh> _ChuongTrinhList;
				
		/// <summary> 
		/// The list of ChuongTrinh to be displayed in the DGV.
		/// </summary>
		[System.ComponentModel.Category("Data")]
		public Entities.TList<Entities.ChuongTrinh> ChuongTrinhList
		{
			get {return this._ChuongTrinhList;}
			set
			{
				this._ChuongTrinhList = value;
				this.uxChuongTrinhBindingSource.DataSource = null;
				this.uxChuongTrinhBindingSource.DataSource = value;
				this.uxChuongTrinhDataGridView.DataSource = null;
				this.uxChuongTrinhDataGridView.DataSource = this.uxChuongTrinhBindingSource;				
				//this.uxChuongTrinhBindingSource.ListChanged += new System.ComponentModel.ListChangedEventHandler(uxChuongTrinhBindingSource_ListChanged);
				this.uxChuongTrinhBindingSource.CurrentItemChanged += new System.EventHandler(OnChuongTrinhBindingSourceCurrentItemChanged);
			}
		}
		
		private void OnChuongTrinhBindingSourceCurrentItemChanged(object sender, System.EventArgs e)
		{
			_currentChuongTrinh = uxChuongTrinhBindingSource.Current as Entities.ChuongTrinh;
			
			if (_currentChuongTrinh != null)
			{
				_currentChuongTrinh.Validate();
			}
			//_ChuongTrinh.Validate();
			OnCurrentEntityChanged();
		}

		//void uxChuongTrinhBindingSource_ListChanged(object sender, System.ComponentModel.ListChangedEventArgs e)
		//{
			//throw new Exception("The method or operation is not implemented.");
		//}		
		
		#endregion
		
		/// <summary>
		/// Gets the currently selected <c cref="Entities.ChuongTrinh"/> instance.
		/// </summary>
		public Entities.ChuongTrinh SelectedChuongTrinh
		{
			get {return this._currentChuongTrinh;}
		}
		
		#region other properties
		
		/// <summary>
		/// Gets or sets a value indicating whether you have provided your own data-management operations for the DataGridView control.
		/// </summary>
		[System.ComponentModel.Category(".netTiers")]
		public bool VirtualMode
		{
			get {return this.uxChuongTrinhDataGridView.VirtualMode;}
			set
			{
				this.uxChuongTrinhDataGridView.VirtualMode = value;
			}
		}
		
		/// <summary>
		/// Gets or sets a value indicating whether the option to add rows is displayed to the user.
		/// </summary>
		[System.ComponentModel.Category(".netTiers")]
		public bool AllowUserToAddRows
		{
			get {return this.uxChuongTrinhDataGridView.AllowUserToAddRows;}
			set {this.uxChuongTrinhDataGridView.AllowUserToAddRows = value;}
		}
		
		/// <summary>
		/// Gets or sets a value indicating whether the user is allowed to delete rows from the DataGridView.
		/// </summary>
		[System.ComponentModel.Category(".netTiers")]
		public bool AllowUserToDeleteRows
		{
			get {return this.uxChuongTrinhDataGridView.AllowUserToDeleteRows;}
			set {this.uxChuongTrinhDataGridView.AllowUserToDeleteRows = value;}
		}
		
		/// <summary>
		/// Gets the <c cref="DataGridView"/> instance.
		/// </summary>
		[System.ComponentModel.Category(".netTiers")]
		public System.Windows.Forms.DataGridView DataGridView
		{
			get {return this.uxChuongTrinhDataGridView;}
		}
		
		/// <summary>
		/// Gets a collection that contains all the columns in the control.
		/// </summary>
		[System.ComponentModel.Category(".netTiers")]		
		[System.ComponentModel.DesignerSerializationVisibilityAttribute(System.ComponentModel.DesignerSerializationVisibility.Visible)]
		//[System.ComponentModel.EditorAttribute("System.Windows.Forms.Design.DataGridViewColumnCollectionEditor, System.Design", typeof(System.Drawing.Design.UITypeEditor))] 
		public System.Windows.Forms.DataGridViewColumnCollection Columns
		{
			get { return this.uxChuongTrinhDataGridView.Columns; }
		}
		
		#endregion
		
		/// <summary> 
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.IContainer components = null;
	
		/// <summary> 
		/// Clean up any resources being used.
		/// </summary>
		/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
		protected override void Dispose(bool disposing)
		{
			if (disposing && (components != null))
			{
				components.Dispose();
			}
			base.Dispose(disposing);
		}
	
		#region Constructor
	
		/// <summary>
		/// Initializes a new instance of the <see cref="ChuongTrinhDataGridViewBase"/> class.
		/// </summary>
		public ChuongTrinhDataGridViewBase()
		{			
			this.components = new System.ComponentModel.Container();			
			
			this.uxChuongTrinhDataGridView = new System.Windows.Forms.DataGridView();
			this.uxChuongTrinhBindingSource = new System.Windows.Forms.BindingSource(this.components);
			this.uxChuongTrinhErrorProvider = new System.Windows.Forms.ErrorProvider(this.components);
			this.uxChuongTrinhIdDataGridViewColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
			this.uxTenChuongTrinhDataGridViewColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
			this.uxIsActiveDataGridViewColumn = new System.Windows.Forms.DataGridViewCheckBoxColumn();
			this.uxLastUpdateDataGridViewColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
			this.uxUpdateByDataGridViewColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
			((System.ComponentModel.ISupportInitialize)(this.uxChuongTrinhDataGridView)).BeginInit();
			((System.ComponentModel.ISupportInitialize)(this.uxChuongTrinhBindingSource)).BeginInit();
			((System.ComponentModel.ISupportInitialize)(this.uxChuongTrinhErrorProvider)).BeginInit();
			this.SuspendLayout();
						
			// 
			// uxChuongTrinhErrorProvider
			// 
			this.uxChuongTrinhErrorProvider.ContainerControl = this;
			this.uxChuongTrinhErrorProvider.DataSource = this.uxChuongTrinhBindingSource;						
			// 
			// uxChuongTrinhDataGridView
			// 
			this.uxChuongTrinhDataGridView.AutoGenerateColumns = false;
			this.uxChuongTrinhDataGridView.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
			this.uxChuongTrinhDataGridView.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
		this.uxChuongTrinhIdDataGridViewColumn,
		this.uxTenChuongTrinhDataGridViewColumn,
		this.uxIsActiveDataGridViewColumn,
		this.uxLastUpdateDataGridViewColumn,
		this.uxUpdateByDataGridViewColumn			});
			this.uxChuongTrinhDataGridView.Dock = System.Windows.Forms.DockStyle.Fill;
			this.uxChuongTrinhDataGridView.Location = new System.Drawing.Point(0, 0);
			this.uxChuongTrinhDataGridView.Name = "uxChuongTrinhDataGridView";
			this.uxChuongTrinhDataGridView.Size = new System.Drawing.Size(470, 300);
			this.uxChuongTrinhDataGridView.TabIndex = 0;	
			this.uxChuongTrinhDataGridView.BackgroundColor = System.Drawing.Color.WhiteSmoke;
			this.uxChuongTrinhDataGridView.EnableHeadersVisualStyles = false;
			this.uxChuongTrinhDataGridView.DataError += new System.Windows.Forms.DataGridViewDataErrorEventHandler(this.OnChuongTrinhDataGridViewDataError);
			this.uxChuongTrinhDataGridView.CellValueNeeded += new System.Windows.Forms.DataGridViewCellValueEventHandler(this.OnChuongTrinhDataGridViewCellValueNeeded);
			this.uxChuongTrinhDataGridView.CellValuePushed += new System.Windows.Forms.DataGridViewCellValueEventHandler(this.OnChuongTrinhDataGridViewCellValuePushed);
			
			//
			// uxChuongTrinhIdDataGridViewColumn
			//
			this.uxChuongTrinhIdDataGridViewColumn.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.DisplayedCells;
			this.uxChuongTrinhIdDataGridViewColumn.DataPropertyName = "ChuongTrinhId";
			this.uxChuongTrinhIdDataGridViewColumn.HeaderText = "ChuongTrinhId";
			this.uxChuongTrinhIdDataGridViewColumn.Name = "uxChuongTrinhIdDataGridViewColumn";
			this.uxChuongTrinhIdDataGridViewColumn.Resizable = System.Windows.Forms.DataGridViewTriState.True;
			this.uxChuongTrinhIdDataGridViewColumn.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
			this.uxChuongTrinhIdDataGridViewColumn.ReadOnly = true;		
			//
			// uxTenChuongTrinhDataGridViewColumn
			//
			this.uxTenChuongTrinhDataGridViewColumn.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.DisplayedCells;
			this.uxTenChuongTrinhDataGridViewColumn.DataPropertyName = "TenChuongTrinh";
			this.uxTenChuongTrinhDataGridViewColumn.HeaderText = "TenChuongTrinh";
			this.uxTenChuongTrinhDataGridViewColumn.Name = "uxTenChuongTrinhDataGridViewColumn";
			this.uxTenChuongTrinhDataGridViewColumn.Resizable = System.Windows.Forms.DataGridViewTriState.True;
			this.uxTenChuongTrinhDataGridViewColumn.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
			this.uxTenChuongTrinhDataGridViewColumn.ReadOnly = false;		
			//
			// uxIsActiveDataGridViewColumn
			//
			this.uxIsActiveDataGridViewColumn.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.DisplayedCells;
			this.uxIsActiveDataGridViewColumn.DataPropertyName = "IsActive";
			this.uxIsActiveDataGridViewColumn.HeaderText = "IsActive";
			this.uxIsActiveDataGridViewColumn.Name = "uxIsActiveDataGridViewColumn";
			this.uxIsActiveDataGridViewColumn.Resizable = System.Windows.Forms.DataGridViewTriState.True;
			this.uxIsActiveDataGridViewColumn.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
			this.uxIsActiveDataGridViewColumn.ReadOnly = false;		
			//
			// uxLastUpdateDataGridViewColumn
			//
			this.uxLastUpdateDataGridViewColumn.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.DisplayedCells;
			this.uxLastUpdateDataGridViewColumn.DataPropertyName = "LastUpdate";
			this.uxLastUpdateDataGridViewColumn.HeaderText = "LastUpdate";
			this.uxLastUpdateDataGridViewColumn.Name = "uxLastUpdateDataGridViewColumn";
			this.uxLastUpdateDataGridViewColumn.Resizable = System.Windows.Forms.DataGridViewTriState.True;
			this.uxLastUpdateDataGridViewColumn.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
			this.uxLastUpdateDataGridViewColumn.ReadOnly = false;		
			//
			// uxUpdateByDataGridViewColumn
			//
			this.uxUpdateByDataGridViewColumn.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.DisplayedCells;
			this.uxUpdateByDataGridViewColumn.DataPropertyName = "UpdateBy";
			this.uxUpdateByDataGridViewColumn.HeaderText = "UpdateBy";
			this.uxUpdateByDataGridViewColumn.Name = "uxUpdateByDataGridViewColumn";
			this.uxUpdateByDataGridViewColumn.Resizable = System.Windows.Forms.DataGridViewTriState.True;
			this.uxUpdateByDataGridViewColumn.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
			this.uxUpdateByDataGridViewColumn.ReadOnly = false;		
			
			// this
			this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
			this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
			this.Controls.Add(this.uxChuongTrinhDataGridView);
			this.Name = "ChuongTrinhDataGridView";
			this.Size = new System.Drawing.Size(470, 300);
			((System.ComponentModel.ISupportInitialize)(this.uxChuongTrinhErrorProvider)).EndInit();
			((System.ComponentModel.ISupportInitialize)(this.uxChuongTrinhDataGridView)).EndInit();
			((System.ComponentModel.ISupportInitialize)(this.uxChuongTrinhBindingSource)).EndInit();
			this.ResumeLayout(false);
		}
		#endregion
				
		#region events
		
		/// <summary>
		/// Raised the CurrentEntityChanged event.
		/// </summary>
		protected void OnCurrentEntityChanged()
		{
			if (CurrentEntityChanged != null)
			{
				ChuongTrinhDataGridViewEventArgs args = new ChuongTrinhDataGridViewEventArgs();
				args.ChuongTrinh = _currentChuongTrinh;
				CurrentEntityChanged(this, args); 
			}
		}
		
		#endregion
		
		/// <summary>
		/// Strongly typed event 
		/// </summary>
		public class ChuongTrinhDataGridViewEventArgs : System.EventArgs
		{
			private Entities.ChuongTrinh	_ChuongTrinh;
	
			/// <summary>
			/// the  Entities.ChuongTrinh instance.
			/// </summary>
			public Entities.ChuongTrinh ChuongTrinh
			{
				get { return _ChuongTrinh; }
				set { _ChuongTrinh = value; }
			}	
		}
		
		/// <summary>
		/// Handles the DataError event of the uxChuongTrinhDataGridView control.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">The <see cref="T:System.Windows.Forms.DataGridViewDataErrorEventArgs"/> instance containing the event data.</param>
		protected void OnChuongTrinhDataGridViewDataError(object sender, System.Windows.Forms.DataGridViewDataErrorEventArgs e)
		{
			System.Diagnostics.Debug.WriteLine(string.Format( "{0}, {1}", e.ColumnIndex, e.RowIndex));
		}
		
		/// <summary>
		/// Handles the CellValueNeeded event of the uxChuongTrinhDataGridView control.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">The <see cref="T:System.Windows.Forms.DataGridViewCellValueEventArgs"/> instance containing the event data.</param>
		protected void OnChuongTrinhDataGridViewCellValueNeeded(object sender, System.Windows.Forms.DataGridViewCellValueEventArgs e)
		{
			switch(uxChuongTrinhDataGridView.Columns[e.ColumnIndex].Name)
			{
					case "uxChuongTrinhIdDataGridViewColumn":
						e.Value = ChuongTrinhList[e.RowIndex].ChuongTrinhId;
						break;
					case "uxTenChuongTrinhDataGridViewColumn":
						e.Value = ChuongTrinhList[e.RowIndex].TenChuongTrinh;
						break;
					case "uxIsActiveDataGridViewColumn":
						e.Value = ChuongTrinhList[e.RowIndex].IsActive;
						break;
					case "uxLastUpdateDataGridViewColumn":
						e.Value = ChuongTrinhList[e.RowIndex].LastUpdate;
						break;
					case "uxUpdateByDataGridViewColumn":
						e.Value = ChuongTrinhList[e.RowIndex].UpdateBy;
						break;
				default:
				break;
			}
		}
		
		/// <summary>
		/// Handles the CellValuePushed event of the uxChuongTrinhDataGridView control.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">The <see cref="T:System.Windows.Forms.DataGridViewCellValueEventArgs"/> instance containing the event data.</param>
		protected void OnChuongTrinhDataGridViewCellValuePushed(object sender, System.Windows.Forms.DataGridViewCellValueEventArgs e)
		{
			switch(uxChuongTrinhDataGridView.Columns[e.ColumnIndex].Name)
			{
					case "uxChuongTrinhIdDataGridViewColumn":
						ChuongTrinhList[e.RowIndex].ChuongTrinhId = (System.Int32)e.Value;
						break;
					case "uxTenChuongTrinhDataGridViewColumn":
						ChuongTrinhList[e.RowIndex].TenChuongTrinh = (System.String)e.Value;
						break;
					case "uxIsActiveDataGridViewColumn":
						ChuongTrinhList[e.RowIndex].IsActive = (System.Boolean)e.Value;
						break;
					case "uxLastUpdateDataGridViewColumn":
						ChuongTrinhList[e.RowIndex].LastUpdate = (System.DateTime)e.Value;
						break;
					case "uxUpdateByDataGridViewColumn":
						ChuongTrinhList[e.RowIndex].UpdateBy = (System.Int32)e.Value;
						break;
				default:
				break;
			}
		}
	}
}
